package com.qk.management.service.impl;



import cn.hutool.core.bean.BeanUtil;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service
@SuppressWarnings("all")
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public void addDept(Dept dept) {
        boolean hasNull = BeanUtil.hasNullField(dept,"id","updateTime","createTime");
        if(hasNull){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public PageResult<Dept> listDepts(String name, Integer status, Integer page, Integer pageSize) {
        deptMapper.selectCount(name, status);
        Integer offset = (page - 1) * pageSize;
        List<Dept> depts = deptMapper.selectByPage(name, status, offset, pageSize);
        return PageResult.<Dept>builder()
                .total(deptMapper.selectCount(name, status))
                .rows(depts)
                .build();
    }

    @Override
    public Integer updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.updateDept(dept);
    }

    @Override
    public Integer deleteDept(Integer id) {
        if(Objects.isNull( id)){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        return deptMapper.deleteById(id);
    }

    @Override
    public List<Dept> getAllDepts() {
        return deptMapper.getAllDepts();
    }

    @Override
    public Dept getById(Integer id) {
        if(Objects.isNull( id)){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        return deptMapper.getById(id);
    }

}
